" 1.lambda表达式是一种匿名函数,有助于编写逻辑清晰的代码
let AnimalGreeting = {animal -> animal . ' says hello'}
echo AnimalGreeting('cat')

" 2.1.vim9之前的lambda表达式格式
" {args -> expr1}
 " 2.2. vim9 lambda表达式格式
 " (args) => expr1
" 2.3.lambda 表达式结果是计算expr1的结果

let AddFunc = {arg1,arg2 -> arg1 + arg2}
echo AddFunc(5,3)

" let SubFunc = (arg1, arg2) => arg1 -arg2
" echo SubFunc(5,3)

" 3.参数是可选的
let Fun ={-> 'error function'}
echo Fun('ignored')
echo Fun()

" 4.l.ambda表达式可以访问范围外的变量和参数,被称为'closure'
" 4.2.变量必须在lamdba表达式定义之前存在
function! Foo(arg)
    let i=3
    return {x -> x+i-a:arg}
endfunction
" 6+3-4 = 5
let Bar = Foo(4)
echo Bar(6)

" 5.lambda 和 closure 支持检查
if has('lambda')
    echo 'lambda support'
endif

" 6.map使用lambda
echo map([1,2,3],{idx,val -> val+1})

" 7. sort使用lambda
echo sort([3,7,2,1,4],{a,b -> a-b})

" 8.Channel, Job and timer使用lambda
let timer = timer_start(500,
                \ {-> execute("echo 'Handler called'","")},
                \ {'repeat': 3})
